home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.gsfc.nasa.gov!usenet
- From: Dirk Broer <Dirk.Broer@gsfc.nasa.gov>
- Newsgroups: comp.lang.c++
- Subject: Re: Interpreting whitespace w. cin
- Date: Fri, 26 Jan 1996 20:00:09 -0800
- Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA
- Message-ID: <3109A349.642D@gsfc.nasa.gov>
- References: <4emb7p$u0@guitar.hal.com>
- NNTP-Posting-Host: control.gsfc.nasa.gov
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0b5 (Win16; I)
-
- Blair Martin wrote:
- >
- > I want to write a program that uses new and delete to dynamically
- > allocate exactly enough free memory to hold string input data.
- >
- > That is, instead of allocating a fixed array of
- > characters from the stack, receive one character at a time and
- > continually allocate, copy, and release space so that the physical
- > and logical lengths of the buffer area are always the same.
- > When the newline character is encountered, print the string
- > and release the space. Then prompt for the next string. When
- > EOF is entered, terminate the program.
- >
- > The problem is with a loop like
- >
- > while (!(cin >> input).eof())
- > {
- > ...
- > }
- >
- > there is no way to know when a newline is encountered since
- > all whitespace is ignored.
- >
- > Is there another way to do this?
- >
- > blair
-
- Try this - not pretty as far as memory usage goes but it might work for
- you:
-
- char buffer[256];
-
- while( !(cin.getline( buffer, 256 ).eof()) )
- {
- strstream temp;
-
- temp << buffer;
- while (!(temp >> input).eof())
- {
- ...
- }
- }
-
- Ok, not pretty - but it basically parses cin line by line and then parses
- the individual lines into tokens.
-
- Dirk
-